package com.ming_za.odupdaterserver.core.thread;

import com.ming_za.odupdaterserver.OdUpdaterServerApplication;

import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class SingleTaskThread {

    private final ThreadPoolExecutor threadPool;

    public SingleTaskThread(String threadName) {

        threadPool = new ThreadPoolExecutor(
                1,
                1,
                Integer.MAX_VALUE,
                TimeUnit.SECONDS,
                new LinkedBlockingQueue<>(),
                new CustomizeNameThreadFactory(threadName));

        OdUpdaterServerApplication.registerStopFunc(unused -> {
            threadPool.shutdown();
            try {
//                System.out.println("尝试关闭"+threadPool+"线程");
                threadPool.awaitTermination(Long.MAX_VALUE,TimeUnit.DAYS);
//                System.out.println(threadPool+"线程已关闭");

            } catch (InterruptedException ignored) {

            }
        });
    }

    public void runTask(Runnable run){
        threadPool.execute(run);
    }
}
